home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / TGE129C.ZIP / PCX2RAW / PCX2RAW.CPP < prev    next >
C/C++ Source or Header  |  1993-03-31  |  3KB  |  145 lines

  1. #include <alloc.h>
  2. #include <dir.h>
  3. #include <process.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "types.h"
  7.  
  8. #define GENERAL_ERR    0
  9. #define OK        1
  10. #define OPEN_ERR    2
  11. #define SEEK_ERR    3
  12. #define READ_ERR    4
  13. #define WRITE_ERR    5
  14. #define MEM_ERR        6
  15.  
  16. int openPcxFile(char *filename, int *wide, int *deep, uchar *palette);
  17. void closePcxFile(void);
  18. int translatePcxFile(FILE *outFile, void *lineBuf);
  19. void assumeExtension(char *filename, char *extension);
  20. void forceExtension(char *filename, char *extension);
  21.  
  22.  
  23. uchar palette[768];
  24.  
  25.  
  26. void main(int argc, char *argv[])
  27. {
  28.   char filename[80];
  29.   uchar *lineBuf;
  30.   int wide, deep;
  31.   FILE *outFile;
  32.  
  33.  
  34.   //*** Print blurb
  35.   printf("PCX2RAW v1.0  Copyright (c) 1993 by Matthew Hildebrand\n\n");
  36.  
  37.   //*** Ensure enough parameters
  38.   if (argc < 2)
  39.   {
  40.     printf("    Usage:  PCX2RAW pcxfile[.pcx]\n\n"
  41.            "    Output:    pcxfile.raw -- bitmap compatible with TGE\n"
  42.            "        pcxfile.pal -- palette compatible with TGE\n\n"
  43.            "    Note:    PCX2RAW will only convert 256-colour PCX files.\n\n");
  44.     exit(1);
  45.   }
  46.  
  47.   //*** Do the job
  48.   strcpy(filename, argv[1]);
  49.   assumeExtension(filename, ".pcx");
  50.   strupr(filename);
  51.  
  52.   //*** Open the PCX file
  53.   if (openPcxFile(filename, &wide, &deep, palette) != OK)
  54.   {
  55.     printf("Error processing file %s\n\n", filename);
  56.     exit(1);
  57.   }
  58.  
  59.   //*** Allocate a line buffer
  60.   if ((lineBuf=(uchar*)malloc(wide)) == NULL)
  61.   {
  62.     closePcxFile();            // close the PCX file
  63.     printf("Out of memory\n\n");
  64.     exit(1);
  65.   }
  66.  
  67.   //*** Create the output RAW file
  68.   forceExtension(filename, ".RAW");
  69.   if ((outFile=fopen(filename,"wb")) == NULL)
  70.   {
  71.     closePcxFile();
  72.     printf("Error creating image file %s\n\n", filename);
  73.     exit(1);
  74.   }
  75.  
  76.   //*** Translate the image data
  77.   printf("Converting image data...");
  78.   fwrite(&wide, 2, 1, outFile);
  79.   fwrite(&deep, 2, 1, outFile);
  80.   if (translatePcxFile(outFile, lineBuf) != OK)
  81.   {
  82.     printf("\n\nError translating formats\n\n");
  83.     exit(1);
  84.   }
  85.  
  86.   //*** Clean up
  87.   free(lineBuf);
  88.   closePcxFile();
  89.   fclose(outFile);
  90.   printf(" Done.\n");
  91.  
  92.   //*** Open palette data file
  93.   forceExtension(filename, ".PAL");
  94.   if ((outFile=fopen(filename,"wb")) == NULL)
  95.   {
  96.     printf("Error creating palette file %s\n\n", filename);
  97.     exit(1);
  98.   }
  99.  
  100.   //*** Write palette data
  101.   if (!fwrite(palette, 768, 1, outFile))
  102.   {
  103.     printf("Error writing palette file %s\n\n", filename);
  104.     fclose(outFile);
  105.     exit(1);
  106.   }
  107.   fclose(outFile);
  108.   printf("Writing palette... Done.\n\n");
  109. }
  110.  
  111.  
  112. //***
  113. //*** Filename manipulation routines
  114. //***
  115.  
  116. void assumeExtension(char *filename, char *defaultExt)
  117. {
  118.   char fileDrive[MAXDRIVE];
  119.   char fileDir[MAXDIR];
  120.   char fileFile[MAXFILE];
  121.   char fileExt[MAXEXT];
  122.   char newPath[MAXPATH];
  123.   int status;
  124.  
  125.   status = fnsplit(filename, fileDrive, fileDir, fileFile, fileExt);
  126.  
  127.   if (!(status&EXTENSION))
  128.     strcpy(fileExt, defaultExt);
  129.  
  130.   fnmerge(newPath, fileDrive, fileDir, fileFile, fileExt);
  131.   strcpy(filename, newPath);
  132. }
  133.  
  134. void forceExtension(char *filename, char *extention)
  135. {
  136.   char fileDrive[MAXDRIVE];
  137.   char fileDir[MAXDIR];
  138.   char fileFile[MAXFILE];
  139.   char fileExt[MAXEXT];
  140.  
  141.   fnsplit(filename, fileDrive, fileDir, fileFile, fileExt);
  142.  
  143.   if (strcmpi(fileExt, extention))
  144.     fnmerge(filename, fileDrive, fileDir, fileFile, extention);
  145. }